﻿using UnityEngine;
using System.Collections;

public class ShotSelectButton : MonoBehaviour, ITouchable, IResettable {

    public GameUIState.ShotType associatedShotType;

    GameUIState selectionControl;

    public float cooldownRate = 2.0f;   // Time in seconds it takes for a shot to become ready
    public float shotReadiness = 1.0f;  // Shots start ready to fire

    public Material shotCharging;
    public Material shotCharged;

    private Transform rechargeBar;
    private Transform rechargeBarVisible;

    public int shotCount = 1;

	// Use this for initialization
	void Start () {

        selectionControl = this.transform.parent.GetComponent<GameUIState>();

        TouchableList.Instance.AddItem(this);
        ResetItemList.Instance.AddItem(this);

        rechargeBar = transform.FindChild("rechargeBar");
        rechargeBarVisible = rechargeBar.FindChild("rechargeBarVisible");

        rechargeBarVisible.renderer.material = (shotReadiness >= 1.0f) ? shotCharged : shotCharging;
    }
	
	// Update is called once per frame
	void Update () {

        if (shotReadiness < 1)
        {
            if (shotCount > 0)
            {
                shotReadiness = Mathf.Lerp(0, 1.0f, shotReadiness + Time.deltaTime / cooldownRate);

                // Set local Y scale on child object
                rechargeBar.localScale = new Vector3(rechargeBar.localScale.x, shotReadiness, rechargeBar.localScale.z);

                rechargeBarVisible.renderer.material = (shotReadiness >= 1.0f) ? shotCharged : shotCharging;
            }
            else
            {
                rechargeBarVisible.renderer.enabled = false;
            }
        }
        else
        {
            rechargeBar.localScale = new Vector3(rechargeBar.localScale.x, 1, rechargeBar.localScale.z);
            rechargeBarVisible.renderer.material = shotCharged;
        }

	}

    public void OnTouch(Vector3 screenPosition, Vector3 worldPosition)
    {
        Select();
    }

    public void Select()
    {
        selectionControl.selectedShotType = this.associatedShotType;
    }

    public bool HoldsTouch()
    {
        return false;
    }

    public void OnRelease(Vector3 screenPosition, Vector3 worldPosition)
    {
        return;
    }

    public GameObject GetColliderObject()
    {
        return this.gameObject;
    }


    public void OnDrag(Vector3 screenPosition, Vector3 worldPosition)
    {
        throw new System.NotImplementedException();
    }

    public bool TouchEnabled { get; set; }



    public void Reset()
    {
        TouchEnabled = true;
        shotReadiness = 1.0f;
    }

    public bool PersistAfterReset()
    {
        return true;
    }

    public void OnShotFired()
    {
        shotReadiness = 0.0f;
        shotCount--;
        if (shotCount <= 0) TouchEnabled = false;
    }

    public bool CanFire()
    {
        return shotReadiness >= 1.0f && shotCount > 0;
    }
}
